<!--
 * @Description: 
 * @Author: william
 * @Date: 2025-06-11 10:19:03
 * @LastEditors: william
 * @LastEditTime: 2025-07-15 16:03:42
-->
<template>
  <uni-easyinput
    ref="refInput"
    v-model="modelValue"
    :placeholder="getPlaceholder()"
    :trim="trim"
    :maxlength="maxlength"
    :clearable="clearable"
    :="$attrs"
    @input="handleInput"
  >
    <template #left>
      <slot name="left"></slot>
    </template>
    <template #right>
      <slot name="right"></slot>
    </template>
  </uni-easyinput>
</template>

<script setup>
defineOptions({
  name: 'Input',
  inheritAttrs: false
})

const props = defineProps({
  verifyType: {
    type: String,
    default: '' // nzint-不以0开头的整数/int-整数/float-浮点数/alnum-字母或数字[a-zA-Z0-9]/idcard-身份证号
  },
  placeholder: {
    type: String,
    default: ''
  },
  label: {
    type: String,
    default: ''
  },
  trim: {
    type: String,
    default: 'all'
  },
  maxlength: {
    type: Number,
    default: 32
  },
  clearable: {
    type: Boolean,
    default: true
  },
  readonly: {
    type: Boolean,
    default: false
  }
})

const modelValue = defineModel({
  type: [String, Number],
  default: ''
})
const emit = defineEmits(['input'])
const refInput = ref(null)

const getPlaceholder = _ => {
  return (
    props.placeholder ||
    g_rules.zhAndEnShowMsg(g_t('public.pleaseInput'), props.label)
  )
}

// 对输入的字符进行过滤
const handleInput = value => {
  const verifyMap = {
    nzint: /^(0+)|[^1-9\^\d]+/g,
    int: /[^\d]+/g,
    float: /[^\d.]+/g,
    alnum: /[^\w]+/g,
    idcard: /[^\d^A-Z]+/g,
    cletter: /[^A-Z]+/g
  }
  nextTick(() => {
    if (props.verifyType) {
      modelValue.value = value.replace(verifyMap[props.verifyType] || '', '')
      emit('input', modelValue.value)
    }
  })
}

defineExpose({
  refInput,
  modelValue
})
</script>

<style lang="scss" scoped></style>
